<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>问卷编辑</title>
    <link rel="stylesheet" href="/static/css/bootstrap.min.css">
    <link rel="stylesheet" href="/static/css/updateSurvey.css">
    <style>
        .invalid::-webkit-input-placeholder {
            color: red;
        }
    </style>
</head>

<body>

    <div id="container">
        <div class="survey_card">

        
        <div class="page">
            <div class="theme_state_label">
                <div class="theme_lable">
                    <input type="text" id="q_theme" value="{{surveys.surveyTheme}}">
                </div>
                <div class="border"></div>
                <div class="state_lable">
                    说明：<input type="text" id="q_state" value="{{surveys.surveyState}}">
                </div>
            </div>

            <div>
                <div class="pageIndex">
                    <table id="istable" class="pageIndex-lable">
                        {% for item in question %}
                        <tr>
                            <td style="width: 4vw; text-align: center;">
                                {{item.questionNumber}}.
                            </td>
                            <td>
                                <input type="text" id="num_{{item.questionNumber}}" value="{{item.questionContent}}">
                            </td>
                            <td>
                                <input type="button" class="remove_input" onclick="removeColumn(this)">
                            </td>
                        </tr>
                        {% endfor %}
                    </table>

                    <div class="btn-css">
                        <input class=" btn btn-outline-primary" type="button" value="创建问题" onclick="addQuestion()">
                        <br>
                        <input class=" btn btn-outline-primary" type="button" value="保存"
                            onclick="reset(istable,'{{surveyId}}');window.onbeforeunload = null;">
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

    <script src="/static/js/jquery-3.6.0.min.js"></script>
    <script src="https://cdn.bootcss.com/axios/0.18.0/axios.min.js"></script>
    <script>
        var unloadPageTip = function () { return "刷新页面有可能会丢失数据"; };
        window.onbeforeunload = unloadPageTip;
    </script>
    <script>
        function reset(istable, surveyId) {
            let currentId = surveyId
            if ($('#q_theme').val().length > 0) {
                if ($('#q_state').val().length > 0) {
                    var arr = [];
                    var judge = "true";
                    for (var i = 1; i <= istable.rows.length; i++) {
                        var a = $(`#num_${i}`).val().trim();
                        if (a.length > 0) {
                            arr.push(a);
                            judge = "true";
                        } else {
                            alert("问卷问题不能为空，请重新输入")
                            judge = "false";
                            break;
                        }
                    }

                    if (judge === "true") {
                        axios({
                            method: 'post',
                            url: '/surveypage/updatedata',
                            data: {
                                content: arr,
                                theme: $('#q_theme').val(),
                                state: $('#q_state').val(),
                                currentId
                            }
                        }).then(function (response) {
                            // axios工具下，服务器真正返回的数据在返回对象的data中
                            let res = response.data;
                            if (res.code === 200) {
                                alert("保存成功")
                                window.location.href = "/surveyAll"
                            }
                        }).catch((err) => {

                        });
                    } else if (judge === "false") {
                        console.log("保存失败");
                    }
                } else {
                    alert("问卷说明不能为空，请重新输入")
                }
            } else {
                alert("问卷主题不能为空，请重新输入")
            }
        }

    </script>

    <script>
        function addQuestion() {
            var re = '';
            var trlen = $('#istable').find('tr').length;
            re = `<tr>
                    <td style="width: 4vw; text-align: center;">
                        ${trlen + 1}.
                    </td>
                    <td>
                        <input type="text" id="num_${trlen + 1}" >
                    </td>
                    <td>
                        <input type="button" class="remove_input" onclick="removeColumn(this)">
                    </td>
                </tr>`;

            $("#istable").append(re);
            sort();
        }

        function removeColumn(thisbutton) {
            $(thisbutton).parent().parent().remove();
            sort();
        }

        function sort() {
            let para = $("#istable tr").length;//获取tbody删除一行后tr的个数
            var arr = [];
            $("[id*='num_']").each(function (index, name) {
                arr.push(index);
                $('#istable input[type=text]').eq(index).attr('id', `num_${index + 1}`);
            });
            console.log(arr);
            for (let i = 0; i <= para; i++) { //循环遍历
                //将遍历后的i给到每行的序号中
                $('#istable').find('tr').eq(i).find('td:first').html(i + 1 + '.');

            }

        }
    </script>
</body>

</html>